package com.jic.interaction.hereWeGo.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.interaction.hereWeGo.common.utlis.HereWeGoHttpUtil;
import com.jic.interaction.hereWeGo.model.entity.AccountChecking;
import com.jic.interaction.hereWeGo.model.entity.Orders;
import com.jic.interaction.hereWeGo.service.AccountCheckingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AccountCheckingServiceImpl
 * @Description TODO
 * @Author 陈鸿锋
 * @Date 2020/11/9 20:27
 * @Version 1.0
 **/
@Service
@Slf4j
public class AccountCheckingServiceImpl implements AccountCheckingService {
    @Value("${route.order}")
    private String url;

/*
 * @Author chengpunan
 *@Package com.jic.interaction.hereWeGo.service.impl
 *ClassName AccountCheckingServiceImpl
 * @Description  //TODO 查询对应⽉份的交易记录
 * @Date 21:15 2020/11/9
 * @Param
 * @return RestResult<?>(List<Orders>) 返回订单信息列表
 * @Version:1.0
 **/
    @Override
    public PageInfo<?> queryAccount(AccountChecking accountChecking, int pageNum, int pageSize) throws Exception {
        log.info("查询订单信息：请求参数{}", accountChecking.toString());
        //请求接口路径
        String url = this.url+"mall/order/"+accountChecking.getAccount()+"/settle";
        //默认值
        pageNum=1;
        pageSize=20;
        //根据parem查询订单信息
        Map<String,String> parem = JSON.parseObject(JSON.toJSONString(accountChecking), Map.class);
        //返回分页信息
        Page<Orders> orders = PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> {
            try {
                HereWeGoHttpUtil.doRequestPost(url, parem);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        //如果当前页面为1 则返回totol
        if(pageNum==1){
            List<Orders> result = orders.getResult();
            long total = orders.getTotal();
            PageResult pageResult=new PageResult();
            pageResult.setRows(result);
            pageResult.setTotal(total);
            return new PageInfo<Orders>(result);
        }
        log.info("查询订单信息结束");
        //返回订单信息集合
        return new PageInfo<Orders>(orders) ;
    }
}
